package com.hunglv.ppn.database;

import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
	private SQLiteDatabase db;

	public static String DB_NAME = "private_phone_db";
	public static int VERSION = 1;

	private static String TABLE_NAME = "data_table";
	private static String KEY_ID = "_id";
	public static int COLUMN_ID = 0;
	private static String KEY_CONTENT = "content";
	public static int COLUMN_CONTENT = 1;
	private static String KEY_TIME = "time";
	public static int COLUMN_TIME = 2;
	private static String KEY_SENDER_PHONE_NUMBER = "sender_phone_number";
	public static int COLUMN_SENDER = 3;
	private static String KEY_TYPE = "type";
	public static int COLUMN_TYPE = 4;

	private static final String TABLE_SMS_CREATE = "create table " + TABLE_NAME
			+ " (" + KEY_ID + " integer primary key, " + KEY_CONTENT
			+ " text, " + KEY_TIME + " long, " + KEY_SENDER_PHONE_NUMBER
			+ " text, " + KEY_TYPE + " int);";

	public static final int SMS_TYPE = 1;
	public static final int CALL_TYPE = 2;

	public DatabaseHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase _db) {
		_db.execSQL(TABLE_SMS_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
		_db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(_db);
	}

	public void open() throws SQLiteException {
		try {
			db = this.getWritableDatabase();
		} catch (SQLiteException e) {
			db = this.getReadableDatabase();
		}
	}

	public void close() {
		db.close();
	}

	public Cursor getAllSmsCursor() {
		Cursor result = db.query(TABLE_NAME, new String[] { KEY_ID,
				KEY_CONTENT, KEY_TIME, KEY_SENDER_PHONE_NUMBER, KEY_TYPE },
				KEY_TYPE + "=" + SMS_TYPE, null, null, null, KEY_TIME + " ASC");
		return result;
	}

	public Cursor getAllCallCursor() {
		Cursor result = db
				.query(TABLE_NAME, new String[] { KEY_ID, KEY_CONTENT,
						KEY_TIME, KEY_SENDER_PHONE_NUMBER, KEY_TYPE }, KEY_TYPE
						+ "=" + CALL_TYPE, null, null, null, KEY_TIME + " ASC");
		return result;
	}

	public long insertNewLog(String content, Calendar time, String phoneNumber,
			int type) {
		// process time to save to database as int
		try {
			int year = time.get(Calendar.YEAR);
			int month = time.get(Calendar.MONTH);
			int day = time.get(Calendar.DAY_OF_MONTH);
			int hour = time.get(Calendar.HOUR_OF_DAY);
			int minute = time.get(Calendar.MINUTE);
			int second = time.get(Calendar.SECOND);

			long currentTime = year * (long) Math.pow(10, 10) + month
					* (long) Math.pow(10, 8) + day * (long) Math.pow(10, 6)
					+ hour * (long) Math.pow(10, 4) + minute * 100 + second;

			ContentValues cv = new ContentValues();
			cv.put(KEY_CONTENT, content);
			cv.put(KEY_TIME, currentTime);
			cv.put(KEY_SENDER_PHONE_NUMBER, phoneNumber);
			cv.put(KEY_TYPE, type);

			return db.insert(TABLE_NAME, null, cv);

		} catch (IllegalArgumentException e) {
		} catch (ArrayIndexOutOfBoundsException e) {
		}
		return -1;
	}
}